/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.youth.fund.hk.hostManager.common.Constants;
import com.youth.fund.hk.hostManager.manager.datamodel.ProductModel;
import com.youth.fund.hk.hostManager.manager.datamodel.RelatedProductModel;
import com.youth.fund.hk.hostManager.manager.service.RelatedProductService;

/**
 * @author Sunk_Sky
 *
 *         2014年12月1日下午3:43:31
 */
@Controller
@RequestMapping("/related")
public class RelatedController
{
	@Autowired
	@Qualifier("RelatedProductService")
	private RelatedProductService service;
	
	@RequestMapping(value = "{id}/updateRelatedList", method = RequestMethod.GET)
	public String list(HttpServletRequest req, @PathVariable("id") int id,
			Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		RelatedProductModel condition = new RelatedProductModel();

		condition.setType_id(id);

		condition.setLang(lang);

		model.addAttribute("relatedList", this.service.query(condition));

		model.addAttribute("type_id", id);

		return "type/related_list";
	}
	@RequestMapping(value = "/{type_id}/create", method = RequestMethod.GET)
	public String showCreateForm(HttpServletRequest req, Model model,
			@PathVariable("type_id") int type_id)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		RelatedProductModel condition = new RelatedProductModel();

		condition.setLang(lang);

		condition.setType_id(type_id);

		model.addAttribute("related", condition);

		model.addAttribute("op", "新增");

		return "type/related_edit";
	}
	@RequestMapping(value = "/{type_id}/create", method = RequestMethod.POST)
	public String create(HttpServletRequest req, RelatedProductModel model,
			RedirectAttributes redirectAttributes)
	{
		int type_id = model.getType_id();

		boolean result = this.service.create(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "新增成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "新增失敗");
		}

		return "redirect:/related/" + type_id + "/updateRelatedList";
	}
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(HttpServletRequest req, @PathVariable int id,
			Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		RelatedProductModel rpm = new RelatedProductModel();

		rpm.setId(id);

		rpm.setLang(lang);

		model.addAttribute("related", rpm);

		model.addAttribute("op", "修改");

		return "type/related_edit";
	}
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(HttpServletRequest req, RelatedProductModel model,
			RedirectAttributes redirectAttributes)
	{
		boolean result = service.update(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "修改失敗");
		}
		int type_id = model.getType_id();

		return "redirect:/related/" + type_id + "/updateRelatedList";
	}
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String showDeleteForm(HttpServletRequest req, @PathVariable int id,
			Model model, RedirectAttributes redirectAttributes)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		RelatedProductModel aum = new RelatedProductModel();

		aum.setId(id);

		aum.setLang(lang);

		boolean result = this.service.delete(aum);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "刪除成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "刪除失敗");
		}

		int type_id = aum.getType_id();
		
		return "redirect:/related/" + type_id + "/updateRelatedList";
	}

}
